package com.oocl.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.common.model.ResponseBean;
import com.oocl.entity.Clients;
import com.oocl.entity.OrderComment;
import com.oocl.entity.Orders;
import com.oocl.service.ClientsService;
import com.oocl.service.OrderCommentService;
import com.oocl.service.OrdersService;
import com.oocl.util.RequestUtil;

@Controller

public class OrderInfoController {
	
	@Resource(name="orderCommentService")
	private OrderCommentService orderCommentService;
	
	@Resource(name="ordersService")
	private OrdersService ordersService;
	
	@Resource(name="clientsService")
	private ClientsService clientsService;
	
	@Value("#{configProperties['communicateurl.complaint']}")
	private String communicateurlcomp;

	@Autowired
	HttpSession ses;
	//评价该订单
	@ResponseBody
	@RequestMapping(value="cmt",method=RequestMethod.POST)
	public OrderComment makeOrderComment(@RequestBody OrderComment cmt,Model model,HttpSession ses){
		String icnum=(String) ses.getAttribute("icnum");
		String cid=(String) ses.getAttribute("cid");
		OrderComment oc=new OrderComment();
		oc=orderCommentService.addOrderComment(icnum, cid, cmt.getComments(), cmt.getOclevel(), cmt.getOid());
		System.out.println(oc.getCtime());
		return oc;
	}
	
	
	
	//暂时没有投诉这个实体，搁置
	//投诉该订单
	@ResponseBody
	@RequestMapping(value="comp",method=RequestMethod.POST)
	public void makeComplaint( String oid, String compreason,HttpSession ses){
		System.out.println(oid);
		System.out.println(compreason);
		
		//String icnum="123456978741852963";
		//要判断这个订单有没有被投诉过
		List<Orders> olist=new ArrayList<Orders>();
		olist=ordersService.updateComplaintStatusByOid(oid);
		
		
		
		String icnum=(String) ses.getAttribute("icnum");
		String cid=(String) ses.getAttribute("cid");
		//String cname="xiaolin";
		Clients client=new Clients();
		client=clientsService.findClientsByCid(cid);
		String cname=client.getCname();
		String params = "micnum=" + icnum + "&cname=" + cname+"&reason=" + compreason;
		ResponseBean rBean = (ResponseBean) RequestUtil.request(communicateurlcomp, RequestUtil.POST, params, ResponseBean.class);
		System.out.println(rBean);
		
		
	}
	
	
	@RequestMapping(value="confirmorder",method=RequestMethod.POST)
	public String confirmOrderandRefresh(String oid,HttpSession session,Model model){
		System.out.println(oid);
		List<Orders> olist=new ArrayList<Orders>();
		olist=ordersService.updateOrderStatus(oid, "4");
		
		//从Session中获取用户id
		String cid=(String)session.getAttribute("cid");
		//cid转client，用于页面显示
		Clients client=new Clients();
		client=clientsService.findClientsByCid(cid);
		System.out.println("用户"+cid);
		
		//刚开始订单生成时，还没有投诉
		model.addAttribute("compstate", 0);
		model.addAttribute("client", client);
		model.addAttribute("orderstate", 4);
		model.addAttribute("sum", olist.get(0).getOsum());
		model.addAttribute("merchant", olist.get(0).getMerchant());
		model.addAttribute("orderid", oid);
		model.addAttribute("info", "订单已完成");
		model.addAttribute("moreinfo", "很高兴能为您提供本次服务");
		model.addAttribute("infolist", olist);
		model.addAttribute("dinfo", olist.get(0).getDeliverInfo());
		model.addAttribute("cmt", null);
		
		return "orderinfo";
	}
	
	
	@RequestMapping(value="cancelorder",method=RequestMethod.POST)
	public String cancelOrderandRefresh(String oid,HttpSession session,Model model){
		System.out.println(oid);
		List<Orders> olist=new ArrayList<Orders>();
		olist=ordersService.updateOrderStatus(oid, "5");
		
		//从Session中获取用户id
		String cid=(String)session.getAttribute("cid");
		//cid转client，用于页面显示
		Clients client=new Clients();
		client=clientsService.findClientsByCid(cid);
		System.out.println("用户"+cid);
		
		//刚开始订单生成时，还没有投诉
		model.addAttribute("compstate", 0);
		model.addAttribute("client", client);
		model.addAttribute("orderstate", 5);
		model.addAttribute("sum", olist.get(0).getOsum());
		model.addAttribute("merchant", olist.get(0).getMerchant());
		model.addAttribute("orderid", oid);
		model.addAttribute("info", "订单已取消");
		model.addAttribute("moreinfo", "期待下次为您提供服务");
		model.addAttribute("infolist", olist);
		model.addAttribute("dinfo", olist.get(0).getDeliverInfo());
		model.addAttribute("cmt", null);
		
		return "orderinfo";
	}
	
	
	@RequestMapping(value="getalls",method=RequestMethod.GET)
	@ResponseBody
	public Object getOneOrdersCenter(){
		String cid=(String) ses.getAttribute("cid");
		// cid="8a5e9d395de55455015de5545aa20000";
		System.out.println("cid" + cid);
		return ordersService.loadOrderCenter(cid);
	}

	@RequestMapping(value="getbystatus",method=RequestMethod.GET)
	@ResponseBody
	public Object getByStatus(String state){
		System.out.println("state...."+state);
		String cid=(String) ses.getAttribute("cid");
		return ordersService.loadOrderCenterBystatus(state, cid);
	}
}
